import http from '@/utils/request'
import type {
  Moment,
  CreateMomentRequest,
  UpdateMomentRequest,
  MomentListRequest,
  MomentListResponse,
  MoodStats,
  TagStats
} from '../types'

// API 基础路径
const API_BASE = '/moments'

export const momentApi = {
  // 获取动态列表
  list: async (params: MomentListRequest): Promise<MomentListResponse> => {
    const queryParams = new URLSearchParams()
    queryParams.append('page', params.page.toString())
    queryParams.append('size', params.size.toString())
    if (params.mood) queryParams.append('mood', params.mood)
    if (params.tag) queryParams.append('tag', params.tag)
    if (params.startDate) queryParams.append('startDate', params.startDate)
    if (params.endDate) queryParams.append('endDate', params.endDate)

    const response = await http.get(`${API_BASE}/list?${queryParams.toString()}`)
    return response.data as MomentListResponse
  },

  // 获取动态详情
  detail: async (id: string): Promise<Moment> => {
    const response = await http.get(`${API_BASE}/${id}`)
    return response.data as Moment
  },

  // 创建动态
  create: async (data: CreateMomentRequest): Promise<Moment> => {
    const response = await http.post(`${API_BASE}/create`, data)
    return response.data as Moment
  },

  // 更新动态
  update: async (data: UpdateMomentRequest): Promise<Moment> => {
    const response = await http.put(`${API_BASE}/update`, data)
    return response.data as Moment
  },

  // 删除动态
  delete: async (id: string): Promise<void> => {
    const response = await http.delete(`${API_BASE}/${id}`)
    return response.data as void
  },

  // 获取心情统计
  getMoodStats: async (): Promise<MoodStats[]> => {
    const response = await http.get(`${API_BASE}/stats/mood`)
    return response.data as MoodStats[]
  },

  // 获取标签统计
  getTagStats: async (): Promise<TagStats[]> => {
    const response = await http.get(`${API_BASE}/stats/tags`)
    return response.data as TagStats[]
  },

  // 获取热门标签
  getPopularTags: async (limit = 20): Promise<string[]> => {
    const response = await http.get(`${API_BASE}/tags/popular?limit=${limit}`)
    return response.data as string[]
  }
}